package top.forforever.srb.core.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.forforever.srb.common.exception.Assert;
import top.forforever.srb.common.result.R;
import top.forforever.srb.common.result.ResponseEnum;
import top.forforever.srb.core.hfb.RequestHelper;
import top.forforever.srb.core.pojo.entity.UserBind;
import top.forforever.srb.core.pojo.vo.UserBindVO;
import top.forforever.srb.core.service.UserBindService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author Mr.Jiang
 * @since 2023-04-29
 */
@RestController
@RequestMapping("/api/core/userBind")
public class ApiUserBindController {

    @Autowired
    private UserBindService userBindService;

    //账户绑定完成后异步通知notify_url，并且返回到商户绑定结果页return_url。
    @PostMapping("notify")
    public String commitBindNotify(HttpServletRequest request) {
        String resultCode = request.getParameter("resultCode");
        String resultMsg = request.getParameter("resultMsg");
        System.out.println("结果编码："+resultCode);
        System.out.println("结果描述："+resultMsg);
        Map<String, Object> resultMap = RequestHelper.switchMap(request.getParameterMap());

        if (!RequestHelper.isSignEquals(resultMap)) {
            return "fail";
        }
        //修改绑定状态
        userBindService.commitBindNotify(resultMap);
        return "success";
    }

    @PostMapping("commitBind")
    public R commitBind(@RequestBody UserBindVO userBindVO, HttpServletRequest request) {
        String userId = request.getHeader("userId");
        String form = userBindService.commitBind(userBindVO,userId);
        return R.ok().data("form",form);
    }

    @GetMapping("getUserBindById")
    public R getUserBindById(HttpServletRequest request) {
        String userId = request.getHeader("userId");
        UserBind userBind = userBindService.getOne(
                new LambdaQueryWrapper<UserBind>()
                        .eq(UserBind::getUserId, userId)
        );
        return R.ok().data("userBind",userBind);
    }

}

